local skels = {}

local yushix = fk.CreateSkill{
    name = "ym2__yushix"
}
Fk:loadTranslationTable{
    ["ym2__yushix"] = "臾弑",
    [":ym2__yushix"] = "出牌阶段开始时，你可以指定一名其他角色，若此阶段你仅对其：①造成了伤害②使用了牌。阶段结束时，若你满足："..
    "<br>0项：你减1点体力上限，视为对其使用【杀】。"..
    "<br>1项：你将一张牌当作【决斗】使用，且改为打出与底牌同类别牌响应。"..
    "<br>2项：你执行一个额外的摸牌与出牌阶段，然后失去1点体力。",
    ["#ym2__yushix-choose"] = "是否发动 臾弑？选择一名其他角色",
    ["@[chara]ym2__yushix-phase"] = "臾弑",
    ["#ym2__yushix-duel"] = "臾弑：将一张牌当【决斗】使用",
    ["#AskForResponseYushix"] = "请打出一张%arg",
    ["#AskForResponseYushixs"] = "请打出一张%arg（此为第 %arg2 张，共需 %arg3 张）",

    ["$ym2__yushix1"] = "有吾在此，解太师烦忧。",
    ["$ym2__yushix2"] = "这些杂兵，我有何惧！",
}
yushix:addEffect(fk.EventPhaseStart,{
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
        local targets = player.room:getOtherPlayers(player)
        return target == player and player:hasSkill(yushix.name) and player.phase == Player.Play and #targets > 0
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets = room:getOtherPlayers(player)
        local to = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = 1,
            skill_name = yushix.name,
            prompt = "#ym2__yushix-choose",
            cancelable = true,
        })
        if #to > 0 then
            event:setCostData(self, { to = to })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).to[1]
        room:setPlayerMark(player,"@[chara]ym2__yushix-phase",to.id)
    end,
})
yushix:addEffect(fk.EventPhaseEnd,{
    can_refresh = function (self, event, target, player, data)
        return player == target and (data.reason == yushix.name or player:getMark("@[chara]ym2__yushix-phase") ~= 0)
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        if data.reason == yushix.name then
            if player.phase == Player.Draw then
                player:gainAnExtraPhase(Player.Play, yushix.name, true)
            else
                room:loseHp(player,1,yushix.name)
            end
        end
        if player:isAlive() and player:getMark("@[chara]ym2__yushix-phase") ~= 0 then
            local n = 0
            local to = room:getPlayerById(player:getMark("@[chara]ym2__yushix-phase"))
            if #player.room.logic:getActualDamageEvents(1,function (e)
                return e.data.from == player and e.data.to == to
            end,Player.HistoryPhase) > 0 and #player.room.logic:getActualDamageEvents(1,function (e)
                return e.data.from == player and e.data.to ~= to
            end,Player.HistoryPhase) == 0 then n = n + 1 end
            if #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                local use = e.data
                return use.from == player and table.contains(use.tos, to)
            end, Player.HistoryPhase) > 0 and #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                local use = e.data
                return use.from == player and not table.contains(use.tos, to)
            end, Player.HistoryPhase) == 0 then n = n + 1 end
            if n == 0 then
                if to:isAlive() then
                    room:changeMaxHp(player,-1)
                    local card = Fk:cloneCard("slash")
                    card.skillName = yushix.name
                    if player:isAlive() and player:canUseTo(card,to,{bypass_times = true, bypass_distances = true}) then
                        room:useCard{
                            card = card,
                            from = player,
                            tos = {to},
                            extra_data = {bypass_times = true, bypass_distances = true},
                        }
                    end
                end
            elseif n == 1 then
                if not player:isNude() then
                    room:askToUseVirtualCard(player, {
                        name = "duel",
                        skill_name = yushix.name,
                        prompt = "#ym2__yushix-duel",
                        cancelable = false,
                        card_filter = {
                            n = 1,
                            cards = player:getCardIds("he"),
                        },
                    })
                end
            else
                player:gainAnExtraPhase(Player.Draw, yushix.name, true)
            end
        end
    end,
})
yushix:addEffect(fk.PreCardEffect,{
    can_refresh = function (self, event, target, player, data)
        return target == player and data.card.trueName == "duel" and data.card.skillName == yushix.name
    end,
    on_refresh = function (self, event, target, player, data)
        local card = data.card:clone()
        local c = table.simpleClone(data.card)
        for k, v in pairs(c) do
            card[k] = v
        end
        card.skill = Fk.skills["ym2__yushix__duel_skill"]
        data.card = card
    end,
})
table.insert(skels,yushix)

local duel_skill = fk.CreateSkill{
    name = "ym2__yushix__duel_skill",
}
duel_skill:addEffect("cardskill",{
    mute = true,
    prompt = "#duel_skill",
    mod_target_filter = function(self, player, to_select, selected, card)
        return to_select ~= player
    end,
    target_filter = Util.CardTargetFilter,
    target_num = 1,
    on_effect = function(self, room, effect)
        local to = effect.to
        local from = effect.from
        local responsers = { to, from }
        local currentTurn = 1
        local currentResponser = to

        local types = {}
        if not effect.card:isVirtual() then
            table.insertIfNeed(types,effect.card:getTypeString())
        else
            for _, cardId in ipairs(effect.card.subcards) do
                table.insertIfNeed(types,Fk:getCardById(cardId):getTypeString())
            end
        end
        local str = table.concat(table.map(types,function (element)
            return Fk:translate(element)
        end),"或")

        while currentResponser:isAlive() do
            local loopTimes = effect:getResponseTimes(currentResponser)

            local respond
            for i = 1, loopTimes do
                local params = { ---@type AskToUseCardParams
                    skill_name = 'duel',
                    pattern = tostring(Exppattern{ cardType = types }),
                    cancelable = true,
                    event_data = effect
                }
                if loopTimes > 1 then
                    params.prompt = "#AskForResponseYushixs:::"..str..":"..i..":"..loopTimes
                else
                    params.prompt = "#AskForResponseYushix:::"..str
                end
                respond = room:askToResponse(currentResponser, params)
                if respond then
                    room:responseCard(respond)
                else
                    break
                end
            end

            if not respond then
                break
            end

            currentTurn = currentTurn % 2 + 1
            currentResponser = responsers[currentTurn]
        end

        if currentResponser:isAlive() then
            room:damage({
                from = responsers[currentTurn % 2 + 1],
                to = currentResponser,
                card = effect.card,
                damage = 1,
                damageType = fk.NormalDamage,
                skillName = duel_skill.name,
            })
        end
    end,
})
table.insert(skels,duel_skill)
return skels